"use strict";
const CONFIG = require('../../../../../common/config');
const moment = require('moment');
const async = require('async');
const luaUtil = require('./luaUtil');
const superagent = require('superagent');
const retUtil = require('./retUtil');
const CONSTANT =  require("../../const/CONST");



const superAgentUtil = (function () {

    function superAgentUtil() {
    }

    /**
     * 访问api并返回数据、错误
     *
     * @author mk
     * @date 2018-06-04
     *
     * @param url api请求路径
     * @param param 请求参数
     * @param {Function} callback 回调函数
     *                      err: 合并失败
     *                      data: 对应请求的结果
     */
    superAgentUtil.reqRes = function(url, param, callback) {
        superagent.get(url)
            .send(param)
            .end(function (error, res) {
                if (error) {
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, null);
                }
                let result = retUtil.safe_parser_JSON_string(res.text);
                if (!result || result.success === 0) {
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, result);
                }
                return callback(null, result);
            });
    };



    return superAgentUtil;
})();

module.exports = superAgentUtil;